---------------------------------------------------------------------------------------
-- RTBB_I2C Init Script
---------------------------------------------------------------------------------------

-- This file must be loaded after the init_base.lua ;

------------------------------------------------------------------------------------------------------------
-- Global Variable
------------------------------------------------------------------------------------------------------------


-- Init SPMI module or not (true)
if (g_init_spmi == nil) then
	g_init_spmi = true
end

-- Specify Slave Address (0x0)
if (g_SPMI_nSlaveAddr == nil) then
	g_SPMI_nSlaveAddr = nil
end

-- Specify SPMI Bus number (0)
if (g_SPMI_nBus == nil) then
	g_SPMI_nBus = 0
end

-- Specify SPMI Bank (nil)
if (g_SPMI_nBank == nil) then
	g_SPMI_nBank = nil
end

-- Specify SPMI Register size  (force 1 byte for now)
-- if (g_SPMI_nRegSize == nil) then
	g_SPMI_nRegSize = 1
-- end

-- Specify SPMI BigEndian or not (true)
if (g_SPMI_bBigEndian == nil) then
	g_SPMI_bBigEndian = true
end



---------------------------------------------------------------------------------------
-- WRITE
---------------------------------------------------------------------------------------

function spmi_w_nbyte(reg_addr, length, data)

	-- Debug
	dbgprintf([[<Write Command:> 
				Slave Address: 0x%x
				Bus: %d
				Bank: 0x%x
				Register Size: %d
				BigEndian: %s
				Register Address: 0x%x
				Data Length: %d
				]], g_SPMI_nSlaveAddr, g_SPMI_nBus, g_SPMI_nBank, g_SPMI_nRegSize, (g_SPMI_bBigEndian) and "true" or "false", reg_addr, length)
	

	dbgprintf("Data: ")
	for i=1,#data,1 do
		dbgprintf("0x%x,",data[i])
	end
	dbgprintf("\n")



	-- Action
	local final_regsize 
	
	if (g_SPMI_nRegSize == 1) then
		if (g_SPMI_nBank ~= nil) then
			
			reg_addr = bit.lshift(g_SPMI_nBank,8) + reg_addr
			final_regsize = 2
		else
			
			final_regsize = 1
		end
	elseif (g_SPMI_nRegSize == 2) then
		if (g_SPMI_nBank ~= nil) then
			
			dbgprintf("Bank is specified when regsize is set to 2")
			return
		else
			
			final_regsize = 2
		end
	end	
	
	
	local nResult = RTBB_EXTSPMI_HLWrite( g_hBridgeBoard, g_SPMI_nBus, g_SPMI_nSlaveAddr, final_regsize, length, reg_addr, data );

	bRet = resultMsg( "RTBB_EXTSPMI_HLWrite", nResult );

	

	
	return bRet
end

function SPMIWV(reg_addr, data, length)

	if( length == nil ) then length = 1 end

	array = {}

	for i=1, length, 1 do
		j = i
		if( g_SPMI_bBigEndian ) then j = length-i+1 end
		array[j] = bit.rshift( data, (i-1)*8 )
		array[j] = bit.band( array[j], 0xff )
	end

	return spmi_w_nbyte( reg_addr, length, array )
end

function SPMIW(reg_addr, data_table)
	if( type(data_table) == "table" ) then
		return spmi_w_nbyte( reg_addr, #data_table, data_table )
	end
end
---------------------------------------------------------------------------------------
-- READ
---------------------------------------------------------------------------------------

function spmi_r_nbyte( reg_addr, length )

	-- Debug
	dbgprintf([[<Read Command:> 
				Slave Address: 0x%x 
				Bus: %d
				Bank: 0x%x
				Register Size: %d
				BigEndian: %s
				Register Address: 0x%x
				Data Length: %d
				]], g_SPMI_nSlaveAddr, g_SPMI_nBus, g_SPMI_nBank, g_SPMI_nRegSize, (g_SPMI_bBigEndian) and "true" or "false", reg_addr, length)

	dbgprintf("\n")
	
	
	
	-- Action	
	local data = {}
	local final_regsize 
	
	if (g_SPMI_nRegSize == 1) then
		if (g_SPMI_nBank ~= nil) then
			
			reg_addr = bit.lshift(g_SPMI_nBank,8) + reg_addr
			final_regsize = 2
		else
			
			final_regsize = 1
		end
	elseif (g_SPMI_nRegSize == 2) then
		if (g_SPMI_nBank ~= nil) then
			
			dbgprintf("Bank is specified when regsize is set to 2")
			return
		else
			
			final_regsize = 2
		end
	end	
	
	
	local nResult = RTBB_EXTSPMI_HLRead( g_hBridgeBoard, g_SPMI_nBus,
		g_SPMI_nSlaveAddr, final_regsize, length, reg_addr, data );

	bRet = resultMsg( "RTBB_EXTSPMI_HLRead", nResult )
	
	
	
	
	return data, bRet;
end

function SPMIR(reg_addr, length)

	if( length == nil ) then length = 1 end
	return spmi_r_nbyte( reg_addr, length )
end

function SPMIRV(reg_addr, length)

	if( length == nil ) then length = 1 end

	local temp_arr, result = spmi_r_nbyte( reg_addr, length )

	if result ~= true then
		return 0, result
	end

	local mul = 1
	local data = 0
	
	for i = 1, length, 1 do
		j = i
		if( g_SPMI_bBigEndian ) then j = length-i+1 end
		data = data + temp_arr[j] * mul
		mul = mul * 256
	end
	
	--dbgprintf("R: 0x%08x\n",data)

	return data, result
end



---------------------------------------------------------------------------------------

function init_module_SPMI()
	

	return true
end

